
package fmu.pi.view.draw.old;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import fmu.pi.model.bean.Usuario;
import fmu.pi.view.handler.HandlerView;

public class LoginTela extends Application 
{
    private Text welcomeText; 
    private Text text; 
    private Text invalidText; 
    private String name;
    private PasswordField password; 
    private Button okButton; 
    private Usuario usuario; 
    private HandlerView handler; 
    
    public LoginTela()
    {
        handler = new HandlerView(); 
        usuario = handler.getUsuario();
        name = usuario.getNome(); 
    }
    
    @Override
    public void start(Stage stage){
        //Formata texto de saudação
        welcomeText = new Text("Bem Vindo!");
        welcomeText.setFont(Font.font("Arial Black", 20)); 
        welcomeText.setFill(Color.BLACK); 
        
        text = new Text("Bem vindo " +name +
                " digite sua senha para entrar no \n só no lucro" );
        text.setFont(Font.font("Arial", 16)); 
        text.setFill(Color.BLACK); 
        
        password = new PasswordField();
        password.setText("senha");
        
        okButton = new Button("Ok");
        okButton.setOnAction(new OkButtonHandler(stage)); 
        
        invalidText = new Text("Senha inválida"); 
        invalidText.setFont(Font.font("Ariel", 16)); 
        invalidText.setVisible(false); 
       
        //Adiciona layout' s e organiza componentes na tela
        HBox southBox = new HBox();
        southBox.setSpacing(10); 
        
        southBox.getChildren().addAll(password, okButton, invalidText);
        
        VBox layout = new VBox(); 
        layout.setSpacing(10); 
        layout.getChildren().addAll(welcomeText, text, southBox); 
        
        stage.setTitle("Login"); 
        stage.setScene(new Scene(layout, 400, 300)); 
        stage.show(); 
        
       
    }
    
    //classe interna para o tratamento de evendos to botão ok
    private class OkButtonHandler implements EventHandler<ActionEvent>
    {
        private Stage playStage; 
        
        public OkButtonHandler(Stage stage)
        {
            playStage = stage; 
        }
        
        @Override
        public void handle(ActionEvent event)
        {
            if(usuario.getSenha().equals(password.getText()))
            {
                TelaPrincipal tela = new TelaPrincipal(); 
                tela.start(playStage); 
            }
            else
                invalidText.setVisible(true); 
          
        }
    }
    
    //apenas para test
   /* public static void main(String[] args) {    
       launch(args);
    }*/
    
}